var adminOn = true;

function launchAdmin() {
  $('.matchWrapper').each(function() {
    var id = $(this).attr('id');
    var tournamentRound = id.split('-')[0];
    var matchOrder = id.split('-')[1];
    addModifyButton($(this), tournamentRound, matchOrder);
    addRemoveButton($(this), tournamentRound, matchOrder);
    addAddingButton($(this), tournamentRound, matchOrder);
  });
}

/**
 * Adds modify button.
 */
function addModifyButton(parent, tournamentRound, matchOrder) {
  var modifyButton = $('<div></div>');
  modifyButton.addClass('adminButton');
  modifyButton.html('<img src="images/admin/bluePenEdit.jpeg" width="16" height="16" />');
  parent.find(".newMatchField").append(modifyButton);
  modifyButton.click(function () {
    openModify(tournamentRound, matchOrder);
  });
}

/**
 * Adds remove button.
 */
function addRemoveButton(parent, tournamentRound, matchOrder) {
  var removeButton = $('<div></div>');
  removeButton.addClass('removeButton adminButton');
  removeButton.html('<img src="images/admin/removeButton.png" width="16" height="16" />');
  parent.find(".newMatchField").append(removeButton);
  removeButton.click(function () {
    openRemove(tournamentRound, matchOrder);
  });
}

/**
 * Adds modify button.
 */
function addAddingButton(parent, tournamentRound, matchOrder) {
  var addingButton = $('<div></div>');
  addingButton.addClass('addingButton adminButton');
  addingButton.html('<img src="images/admin/addButton.jpeg" width="16" height="16" />');
  parent.find(".newMatchField").append(addingButton);
  addingButton.click(function () {
    addMatch(tournamentRound, matchOrder);
  });
}

/**
 * @param tournamentRound
 * @param matchOrder
 * @param idTournamentDraw
 */
function openModify(tournamentRound, matchOrder) {
  $.get('admin/tournamentDraw/getMatchForm.php?idTournamentRoundType=' + tournamentRound + 
      '&matchOrder=' + matchOrder + '&idTournamentDraw=' + idTournamentDraw,
    function(data) {
      $('#adminTournamentDraw').html(data);
      $('#adminTournamentDraw').find('#player1').focus();
  });
}

/**
 * Opens the popin to confirm to remove a match.
 */
function openRemove(tournamentRound, matchOrder) {
  $('#removeMatchConfirmationModal').find('input[name=matchOrder]').val(matchOrder);
  $('#removeMatchConfirmationModal').find('input[name=matchRound]').val(tournamentRound);
  $("#removeMatchConfirmationModal").modal();
}

/**
 * Call ajax to edit the match.
 */
function editMatch() {
  $.post('admin/tournamentDraw/editMatch.php',
      $('#editMatchWrapper form').serialize(), function(data) {
    $('#outputEditMatch').html(data);
    launchDrawDisplay();
  });
}

/**
 * Call ajax to remove the match.
 */
function removeMatch() {
  $.post('admin/tournamentDraw/removeMatch.php',
      $('#removeMatchConfirmationModal form').serialize(), function(data) {
    $('#removeMatchConfirmationModal .close').click();
    $('#outputRemoveMatchConfirmationForm').html(data);
    launchDrawDisplay();
  });
}

/**
 * Call ajax to add a match.
 * @param tournamentRound
 * @param matchOrder
 */
function addMatch(tournamentRound, matchOrder) {
  $.post('admin/tournamentDraw/addMatch.php?idTournamentRoundType='
      + tournamentRound + '&matchOrder=' + matchOrder
      + '&idTournamentDraw=' + idTournamentDraw,
      $('#removeMatchConfirmationModal form').serialize(), function(data) {
    $('#resultWrapperDebug').html(data);
    launchDrawDisplay();
  });
}


function closeMatchEditor() {
  $(editMatchWrapper).remove();
}

/**
 * Ajax call to add a player.
 */
function addPlayer() {
  // $('#addPlayerModal button.close').click();
  $.post('admin/tournamentDraw/addPlayer.php',
      $('#addPlayerModal form').serialize(), function(data) {
    $('#outputAddPlayerForm').html(data);
    $('#editMatchWrapper #refreshEditor').click();
  });
}

/**
 * Ajax call to add a tourament draw.
 */
function addTournamentDraw() {
  $.post('admin/tournamentDraw/addTournamentDraw.php',
    $('#addTournamentDrawModal form').serialize(),
    function(data) {
        $('#outputAddTournamentDrawForm').html(data);
        $.get('admin/tournamentDraw/pageWrapper.php?page=page/tournamentList',
            function(data) {
          $('#tournamentListWrapper').html(data);
        });
  });
}

/**
 * Ajax call to add a tournament.
 */
function addTournament() {
  $.post('admin/tournamentDraw/addTournament.php',
    $('#addTournamentModal form').serialize(),
    function(data) {
        $('#outputAddTournamentForm').html(data);
        $.get('admin/tournamentDraw/pageWrapper.php?page=page/tournamentList',
            function(data) {
          window.reload();
        });
  });
}

/**
 * Ajax call to add a tournament round.
 */
function addTournamentRound() {
  $.post('admin/tournamentDraw/addTournamentRound.php',
    $('#addTournamentRoundModal form').serialize(),
    function(data) {
        $('#outputAddTournamentRoundForm').html(data);
        launchDrawDisplay();
  });
}